﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace TownsModder
{
    /// <summary>
    ///     Interaction logic for SelectFromCollectionWindow.xaml
    /// </summary>
    public partial class SelectFromCollectionWindow : Window
    {
        public SelectFromCollectionWindow(IEnumerable collection, IEnumerable<object> selectedItems = null,
            int maxSelections = int.MaxValue)
        {
            MaxSelections = maxSelections;
            InitializeComponent();

            OriginalItems = new List<SelectableItem>();
            IList<object> enumerable = selectedItems as IList<object> ?? selectedItems.ToList();
            foreach (object c in collection)
            {
                var item = new SelectableItem(c);
                if (enumerable.Contains(c))
                    item.IsChecked = true;
                OriginalItems.Add(item);
            }

            // Quick copy
            SelectableItems = new List<SelectableItem>(OriginalItems);
            SelectableItems = SelectableItems.OrderBy(k => k.Name).OrderByDescending(k => k.IsChecked).ToList();
            lstItems.ItemsSource = SelectableItems;
        }

        private List<SelectableItem> SelectableItems { get; set; }
        private List<SelectableItem> OriginalItems { get; set; }
        public int MaxSelections { get; set; }

        public IEnumerable<object> GetSelectedItems()
        {
            return from selectableItem in SelectableItems
                   where selectableItem.IsChecked
                   select selectableItem.Object;
        }

        private int SelectedItemsCount()
        {
            int count = 0;
            foreach (SelectableItem selectableItem in SelectableItems)
            {
                if (selectableItem.IsChecked)
                    count++;
            }
            return count;
        }

        private void HandleFilterTextChanged(object sender, TextChangedEventArgs e)
        {
            string text = FilterText.Text;
            if (string.IsNullOrWhiteSpace(text))
            {
                SelectableItems = new List<SelectableItem>(OriginalItems);
            }
            else
            {
                SelectableItems = OriginalItems.Where(i => i.Name.Contains(text) || i.IsChecked).ToList();
            }

            SelectableItems = SelectableItems.OrderBy(k => k.Name).OrderByDescending(k => k.IsChecked).ToList();
            lstItems.ItemsSource = SelectableItems;
        }

        private void HandleCheckboxChecked(object sender, RoutedEventArgs e)
        {
            if (SelectedItemsCount() > MaxSelections)
            {
                MessageBox.Show("You can only select " + MaxSelections + " items.",
                    "Error",
                    MessageBoxButton.OK,
                    MessageBoxImage.Error);
                (sender as CheckBox).IsChecked = false;
            }

            SelectableItems = SelectableItems.OrderBy(k => k.Name).OrderByDescending(k => k.IsChecked).ToList();
            lstItems.ItemsSource = SelectableItems;

            Title = "Select Item(s) - " + SelectedItemsCount() + " selected.";
        }

        public class SelectableItem
        {
            public SelectableItem(object o)
            {
                Object = o;
            }

            public object Object { get; set; }
            public string Name { get { return Object.ToString(); } }
            public bool IsChecked { get; set; }
        }
    }
}